//
//  HCBuyGoodsController.m
//  HappyChildren
//
//  Created by jiayibai-02por on 2017/6/14.
//  Copyright © 2017年 WillyZhao. All rights reserved.
//

#import "HCBuyGoodsController.h"
#import "OrderView.h"

@interface HCBuyGoodsController ()
{
    //需要上传的参数
    NSString *goodsID_; //商品id
    NSString *addressID_; //收货地址
    NSString *goodsSize_; //商品尺寸
    NSString *goodsColor_; //商品颜色
    NSString *goodsSum_; //商品数量
}

@property (nonatomic, strong) OrderView *orderView;

@end

@implementation HCBuyGoodsController

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
//    centerView.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
 
    [super viewWillDisappear:animated];
    
//    centerView.hidden = NO;
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"currentAddress" object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.textTitle = @"确认订单";
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self initOrderView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(postCurrentAddress:) name:@"currentAddress" object:nil];
    
    NSDictionary *dataDic = self.parameterObject;
    NSString *address = [dataDic objectForKey:@"address"];
    NSString *recipient = [dataDic objectForKey:@"recipient"];
    NSString *phone = [dataDic objectForKey:@"phone"];
    [self.orderView setOrderWithAddress:address recipient:recipient phone:phone];
    self.orderView.dataDic = dataDic;
    self.dataRequest.delegate = self;
    [self setParameter];
}

- (void)setParameter {

    NSDictionary *parameter = self.parameterObject;
    goodsID_ = parameter[@"qid"];
    goodsColor_ = parameter[@"color"];
    goodsSize_ = parameter[@"size"];
    addressID_ = parameter[@"id"];
    goodsSum_ = parameter[@"sum"];
}

- (void)postCurrentAddress:(NSNotification *)obj {

    NSDictionary *dic = [obj userInfo];
    NSDictionary *address = [dic objectForKey:@"address"];
    NSLog(@"传过来的地址：%@",address);
    NSString *currentAddress = [NSString stringWithFormat:@"%@",[address objectForKey:@"address"]];
    NSString *phone = [NSString stringWithFormat:@"%@",[address objectForKey:@"phone"]];
    NSString *recipient = [NSString stringWithFormat:@"%@",[address objectForKey:@"name"]];
    NSString *aid = [NSString stringWithFormat:@"%@",[address objectForKey:@"id"]];
    addressID_ = aid;
    if ([PublicFunction isNull:currentAddress]) {
        
        return;
    }
    [self.orderView setOrderWithAddress:currentAddress recipient:recipient phone:phone];
}

- (void)initOrderView {

    self.orderView = [[OrderView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-64)];
    
    __weak typeof (self)weakSelf = self;
    weakSelf.orderView.di_clicked = ^(){
        NSLog(@"配送方式");
    };
    
    weakSelf.orderView.ad_clicked = ^(){
        NSLog(@"地址选择");
        
        [weakSelf pushChildrenViewController:@"HCAddressManagerController" parameterObject:nil dataObject:@"选择地址"];
    };
    
    weakSelf.orderView.im_clicked = ^(NSString *aa){

        //立即支付
        if ([PublicFunction isNull:addressID_]) {
            
            [XHToast showBottomWithText:@"请选择收货地址"];
            return;
        }
        if ([PublicFunction isNull:goodsSize_] || [PublicFunction isNull:goodsColor_]) {
            
            [XHToast showTopWithText:@"啊哦～该商品被抢空了"];
            return;
        }
        [self.dataRequest post_HCBuyGoodsWithDic:[self setData]];
    };
    
    weakSelf.orderView.pr_clicked = ^(){
        NSLog(@"优惠积分卡");
    };
    
    [self.centerView addSubview:self.orderView];
}

- (NSDictionary *)setData {

    NSMutableDictionary *dic = [NSMutableDictionary dictionary];

    [dic setObject:addressID_ forKey:@"aid"];
    [dic setObject:goodsID_ forKey:@"gid"];
    [dic setObject:goodsSum_ forKey:@"num"];
    if (![PublicFunction isNull:goodsSize_]) {
        
        [dic setObject:goodsSize_ forKey:@"size"];
    }
    if (![PublicFunction isNull:goodsColor_]) {
        
        [dic setObject:goodsColor_ forKey:@"color"];
    }
    [dic setObject:HCGETUSER(@"userid") forKey:@"uid"];
    return dic;
}

#pragma mark -- request Delegate
- (void)dataRequesterFinish:(DataRequester *)requester requesterWithType:(DataRequestType)aType receiveDictionary:(NSDictionary *)dictionary {

    if (aType == HC_BUYGOODS_TYPE) {
        
        if ([dictionary[@"stat"] integerValue] == 200) {
            [self pushChildrenViewController:@"HCMineExchangeViewController" parameterObject:nil dataObject:nil];
            [XHToast showBottomWithText:@"购买成功"];
        }
        if ([dictionary[@"stat"] integerValue] == 600) {
            
            [XHToast showBottomWithText:@"积分不足"];
        }
        if ([dictionary[@"stat"] integerValue] == 800) {
            
            [XHToast showBottomWithText:@"库存不足"];
        }
    }
}

- (void)dataRequesterFail:(DataRequester *)requester requesterWithType:(DataRequestType)aType receiveErrorDictionary:(NSDictionary *)dictionary {

    if (aType == HC_BUYGOODS_TYPE) {
        
        NSLog(@"购买失败");
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
